<template>
  <section class="f-goods-all">
    <div class="w-card my-content-box">
      <div class="title-box">
        <div class="g-flex-ct">
          <span class="title-text">平台库存列表</span>
        </div>
        <el-button type="warning" plain @click="exportHandle()">导出</el-button>
      </div>
      <div class="my-search-box">
        <div>
          <span>编号：</span>
          <el-input v-model="searchForm.deviceNo" clearable placeholder="请输入编号" />
        </div>
        <div>
          <span>激活号码：</span>
          <el-input v-model="searchForm.activePhone" clearable placeholder="请输入激活号码" />
        </div>
        <div>
          <span>ICCID：</span>
          <el-input v-model="searchForm.iccid" clearable placeholder="请输入ICCID" />
        </div>
        <div>
          <span>短号：</span>
          <el-input v-model="searchForm.shortNo" clearable placeholder="请输入短号" />
        </div>
      </div>
      <div class="my-search-box">
        <div>
          <span>代理商名称：</span>
          <el-input v-model="searchForm.shopName" clearable placeholder="请输入代理商名称" />
        </div>
        <div>
          <span>激活状态：</span>
          <el-select v-model="searchForm.activeFlag" clearable>
            <el-option label="已激活" value="1" />
            <el-option label="未激活" value="0" />
          </el-select>
        </div>
        <div>
          <span>调拨状态：</span>
          <el-select v-model="searchForm.saled" clearable>
            <el-option label="已调拨" value="1" />
            <el-option label="未调拨" value="0" />
          </el-select>
        </div>
        <div class="f-ml-20">
          <el-button @click="searchHandle" type="primary">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </div>
      </div>

      <div class="my-table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'12px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="deviceId" label="设备ID" align="center">
            <template v-slot="scope">
              {{ scope.row.deviceId || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="deviceNo" label="编号" align="center">
            <template v-slot="scope">
              {{ scope.row.deviceNo || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="iccid1" label="ICCID&短号(1)" align="center">
            <template v-slot="scope">
              <p>ICCID：{{ scope.row.iccid1 || '--' }}</p>
              <p>短号：{{ scope.row.shortNo1 || '--' }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="iccid2" label="ICCID&短号(2)" align="center">
            <template v-slot="scope">
              <p>ICCID：{{ scope.row.iccid2 || '--' }}</p>
              <p>短号：{{ scope.row.shortNo2 || '--' }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="iccid3" label="ICCID&短号(3)" align="center">
            <template v-slot="scope">
              <p>ICCID：{{ scope.row.iccid3 || '--' }}</p>
              <p>短号：{{ scope.row.shortNo3 || '--' }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="activeFlagName" label="激活信息" align="center">
            <template v-slot="scope">
              <div v-if="scope.row.activeFlag">
                <p>{{ scope.row.activeFlagName  }}</p>
                <p>{{ scope.row.activeName }}（{{ scope.row.activePhone  }}）</p>
                <p>{{ getDateTime(scope.row.activeTime)[6]   }}</p>
              </div>
              <div v-else>
                <el-link type="warning" style="font-size: 12px;"
                  @click="settingHandle(scope.row.deviceId)">{{ scope.row.activeFlagName  }}</el-link>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="saled" label="调拨状态" align="center">
            <template v-slot="scope">
              {{ scope.row.saled == 1? '已调拨': '未调拨' }}
            </template>
          </el-table-column>
          <el-table-column prop="shopName" label="代理商" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName }}
            </template>
          </el-table-column>
          <el-table-column prop="createDate" label="入库时间" align="center">
            <template v-slot="scope">
              {{ scope.row.createDate || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="updateDate" label="出库时间" align="center">
            <template v-slot="scope">
              {{ scope.row.updateDate|| '--' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="my-table-footer">
          <!-- <div class="footer_btn_box">
              <el-button type="danger" plain size="small">批量收回</el-button>
              <el-button type="primary" plain size="small">批量退库</el-button>
            </div> -->
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>

    <!-- 确认密码 -->
    <ConfirmPass ref="confirmPasswordRef" @verification="confirmSuccess" />

    <router-view></router-view>
  </section>
</template>
    
    <script>
import { onMounted, reactive, ref, toRefs, watch, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import Upload from '@/components/Upload.vue'
import ConfirmPass from '@/components/confirmPass.vue'
import api from '../api'

export default {
  components: { Upload, ConfirmPass },

  setup() {
    const route = useRoute()
    const router = useRouter()

    const confirmPasswordRef = ref(null)

    const state = reactive({
      wareGoodsId: route.query.wareGoodsId,
      equipmentType: route.query.equipmentType,
      color: route.query.color,

      searchForm: {
        "createTimeMax": "",
        "createTimeMin": "",
        "deviceNo": "",
        "iccid": route.query.iccid,
        "orderBy": "id desc",
        "saled": '', // 分配状态，0-未分配，1-已分配
        "shopId": "",
        "shortNo": "",
        "updateTimeMax": "",
        "updateTimeMin": "",
        "wareGoodsId": route.query.wareGoodsId
      },

      tableHeight: 0,
      tableData: [],
      page: 1,
      size: 10,
      total: 0,

      selectDeviceId: ''
    })

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        "createTimeMax": "",
        "createTimeMin": "",
        "deviceNo": "",
        "iccid": route.query.iccid,
        "orderBy": "id desc",
        "saled": '', // 分配状态，0-未分配，1-已分配
        "shopId": "",
        "shortNo": "",
        "updateTimeMax": "",
        "updateTimeMin": "",
        "wareGoodsId": route.query.wareGoodsId
      }
      searchHandle()
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
      }
      api.warehouseInfoList(obj).then(res => {
        state.tableData = res.data.list
        state.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    // 导出
    const exportHandle = () => {
      const obj = {
        responseType: 'blob',
        ...state.searchForm,
      }
      api.warehouseListDown(obj).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    function settingHandle(deviceId) {
      state.deviceId = deviceId
      const title = "手动同步提示"
      const msg = '您确定要将所选的设备手动同步吗？'
      confirmPasswordRef.value.dialogShow(title, msg)
    }

    const confirmSuccess = () => {
      api.warehouseSyncOne({ deviceId: state.deviceId }).then(res => {
        successDeal('操作成功！')
        getTableData()
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 330
      getTableData()
    })

    return {
      ...toRefs(state),
      confirmPasswordRef,
      searchHandle,
      resetHandle,
      handleCurrentChange,
      handleSizeChange,
      getDateTime,
      exportHandle,
      settingHandle,
      confirmSuccess,
    }
  }
}
    </script>
    
    <style lang="scss" scoped>
.f-goods-all {
  .my-content-box {
    box-sizing: border-box;
    padding: 20px;

    .title-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .title-text {
        font-size: 16px;
        font-weight: bold;
      }
    }

    .f-title-desc {
      display: flex;
      align-items: center;
      font-size: 14px;
      margin-left: 40px;
      gap: 0 40px;
      li {
        display: flex;
        align-items: center;
        p:first-child {
          color: #707070;
        }
      }
    }

    .my-search-box {
      box-sizing: border-box;
      padding: 20px 20px 20px 0;
      padding-bottom: 0;
      font-size: 14px;
      display: flex;
      align-items: center;
      gap: 10px;

      > div {
        width: 300px;
        display: flex;
        align-items: center;

        > span {
          flex-shrink: 0;
          width: 90px;
          text-align: right;
        }
      }

      .datebox {
        width: 400px;
      }
    }

    .my-table-box {
      margin-top: 20px;

      .table-handle-box {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .my-table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
        flex-wrap: wrap;
        gap: 10px;
      }
    }
  }
}
</style>